package com.flashcash.solu.view.certification;

import android.content.Context;
import android.content.Intent;

import com.flashcash.kotlin.fragment.certfication.CertifyFragment;
import com.flashcash.solu.R;
import com.flashcash.solu.app.base.BaseActivity;
import com.flashcash.solu.common.EventCollection;
import com.flashcash.solu.view.MainActivity;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;

public class CertifyActivity extends BaseActivity<CertificationPreImp> {

    private static final String KEY_LOANING = "loaning";
    private static final String KEY_START_MAIN_ACTIVITY = "start_main_activity";
    private static final String KEY_ADD_USER = "add_user";
    private static final String KEY_SPLASH = "splash";

    private boolean gotoLoaning;
    private boolean gotoMainActivity;
    private boolean addUser;
    private boolean fromSplash;//从开屏页面跳转到这里来

    public boolean isGotoLoaning() {
        return gotoLoaning;
    }

    public boolean isGotoMainActivity() {
        return gotoMainActivity;
    }

    public boolean isFromSplash() {
        return fromSplash;
    }

    public static void startCertifyActivity(Context context) {
        context.startActivity(new Intent(context, CertifyActivity.class));
    }

    /**
     * 用户填写完成四步认证后，则结束掉CertifyActivity并跳转到MainActivity
     *
     * @param context
     * @param addUser
     */
    public static void startCertifyActivityAndStartMainActivity(Context context, boolean addUser) {
        context.startActivity(new Intent(context, CertifyActivity.class).putExtra(KEY_START_MAIN_ACTIVITY, true).putExtra(KEY_ADD_USER, addUser));
    }

    public static void startCertifyActivityAndStartMainActivityFromSplash(Context context) {
        context.startActivity(new Intent(context, CertifyActivity.class).putExtra(KEY_SPLASH, true));
    }

    /**
     * 跳转到该Activity并检查用户提交资料情况，全部提交成功后，则跳转到贷款详情页面
     *
     * @param context
     */
    public static void startCertifyActivityAndGotoLoaning(Context context) {
        context.startActivity(new Intent(context, CertifyActivity.class).putExtra(KEY_LOANING, true));
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_certify;
    }

    @Override
    protected void init() {
        RxBus.get().register(this);
        if (getIntent() != null) {
            gotoLoaning = getIntent().getBooleanExtra(KEY_LOANING, false);
            gotoMainActivity = getIntent().getBooleanExtra(KEY_START_MAIN_ACTIVITY, false);
            addUser = getIntent().getBooleanExtra(KEY_ADD_USER, false);
            fromSplash = getIntent().getBooleanExtra(KEY_SPLASH, false);
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.container, new CertifyFragment()).commitAllowingStateLoss();
    }

    @Override
    protected CertificationPreImp initPresenterImpl() {
        return new CertificationPreImp();
    }

    /**
     * 完成四步认证
     *
     * @param event
     */
    @Subscribe
    public void onFinishCertify(EventCollection.FirstFinishCertifyEvent event) {
        //跳转到MainActivity
        MainActivity.startMainActivityAndShowCreditTips(this, addUser);
        finish();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }
}
